package com.ynet.middleground.contract.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.contract.bean.*;
import com.ynet.middleground.contract.dto.UserSealDTO;
import com.ynet.middleground.contract.model.SealModel;
import com.ynet.middleground.contract.service.SealService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 印模接口服务
 * @date 2020-04-20 13:46
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "印模接口服务", tags = {"印模接口服务"})
@Path("seal")
@Service(timeout = 6000, version = "0.0.1")
public class SealServiceImpl implements SealService, GenericService {

    @Autowired
    SealModel sealModel;

    /**
     * 创建个人客户的模板印章
     *
     * @param addPersonTemplateSealReq 创建个人印章模板请求类
     * @return 电子印章图片 base64 数据
     */
    @POST
    @Path("person")
    @ApiOperation(value = "e 签宝-创建个人客户的模板印章", notes = "使用 e 签宝个人账户，创建个人客户的模板印章")
    @ApiImplicitParam(name = "requestObj", dataType = "AddPersonTemplateSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> addPersonTemplateSeal(@Valid AddPersonTemplateSealReq addPersonTemplateSealReq) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(sealModel.addPersonTemplateSeal(addPersonTemplateSealReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "创建个人客户的模板印章服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 创建企业客户的模板印章
     *
     * @param addOrganizeTemplateSealReq 创建企业印章模板请求类
     * @return 电子印章图片 base64 数据
     */
    @POST
    @Path("organ")
    @ApiOperation(value = "e 签宝-创建企业客户的模板印章", notes = "使用 e 签宝企业账户，创建企业客户的模板印章")
    @ApiImplicitParam(name = "requestObj", dataType = "AddOrganizeTemplateSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> addOrganizeTemplateSeal(@Valid AddOrganizeTemplateSealReq addOrganizeTemplateSealReq) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(sealModel.addOrganizeTemplateSeal(addOrganizeTemplateSealReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "创建企业客户的模板印章服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 保存自定义印模图片
     *
     * @param addSealReq 添加自定义印章模板请求类
     * @return 保存后的 id
     */
    @POST
    @ApiOperation(value = "保存自定义印模图片", notes = "保存自定义印模图片，用户可自定义印模图片，并将图片进行 Base64 编码")
    @ApiImplicitParam(name = "requestObj", dataType = "AddSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Integer> addSeal(@Valid AddSealReq addSealReq) {
        ServiceResult<Integer> result = new ServiceResult<>();
        try {
            result.setResult(sealModel.addSeal(addSealReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "保存自定义印模图片印章服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 根据用户 id 查询用户所有印模列表
     *
     * @param queryUserSealReq 查询用户印模列表请求类
     * @return 印模列表
     */
    @POST
    @Path("list")
    @ApiOperation(value = "查询用户所有印模列表", notes = "查询用户所有印模列表")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryUserSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<UserSealDTO>> listUserSeal(@Valid QueryUserSealReq queryUserSealReq) {
        ServiceResult<List<UserSealDTO>> result = new ServiceResult<>();
        try {
            result.setResult(sealModel.listUserSeal(queryUserSealReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询用户所有印模列表服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 根据印模 id 查询印模数据
     *
     * @param userSealReq 查询印模详情请求类
     * @return 用户印模信息
     */
    @POST
    @Path("detail")
    @ApiOperation(value = "查询印模详情数据", notes = "根据印模 id 查询印模数据")
    @ApiImplicitParam(name = "requestObj", dataType = "UserSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult<UserSealDTO> getUserSeal(@Valid UserSealReq userSealReq) {
        ServiceResult<UserSealDTO> result = new ServiceResult<>();
        try {
            result.setResult(sealModel.getUserSeal(userSealReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询印模详情数据服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 删除印模
     *
     * @param deleteUserSealReq 删除印模请求
     * @return 结果标志，以 success 字段值标识 true/false
     */
    @DELETE
    @ApiOperation(value = "删除印模", notes = "根据印模 id 删除印模")
    @ApiImplicitParam(name = "requestObj", dataType = "UserSealReq", paramType = "body", required = true)
    @Override
    public ServiceResult deleteUserSeal(@Valid UserSealReq userSealReq) {
        ServiceResult result = new ServiceResult<>();
        try {
            sealModel.deleteUserSealById(userSealReq);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "删除印模服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

}
